package com.ctg.itrdc.imageconsole.data.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Component;

import com.ctg.itrdc.imageconsole.data.dao.IClusterConfigDao;
import com.ctg.itrdc.imageconsole.data.entity.ClusterConfig;
import com.ctg.itrdc.paspsdk.data.dao.impl.BaseDaoImpl;
import com.ctg.itrdc.paspsdk.data.entity.ClassInfoUtils;

/**
 * 
 * @description:
 * @version: v1.0.0
 * @author: Piao.Wu
 * 
 *          Modification History: Date Author Version Description
 *          -----------------------------------------------------------------
 *          2016年9月9日 下午5:24:02 Piao.Wu v1.0.0 create
 *
 */
@Component("clusterConfigDao")
public class ClusterConfigDaoImpl extends BaseDaoImpl implements IClusterConfigDao {

	public List<ClusterConfig> jdbcFindList(Long currentUserId, Boolean isDockerManager) {
		List<ClusterConfig> clustersCreateBySelf = new ArrayList<>();
		if (isDockerManager) {
			String sql = "select * from cluster_config";
			return super.jdbcFindList(sql, ClusterConfig.class, new ArrayList<Object>());
		} else {
			ClusterConfig clusterConfig = new ClusterConfig();
			clusterConfig.setCreateUserId(currentUserId);
			clustersCreateBySelf = super.selectByExample(clusterConfig);
		}
		
		String sql = "SELECT cc.* from cluster_config cc where cc.id in"
				+ " (SELECT uca.cluster_config_id from user_cluster_assign uca where uca.user_id=?) ORDER BY cc.create_time DESC";
		
		List<Object> params = new ArrayList<Object>();
		params.add(currentUserId);
		
		List<ClusterConfig> clustersCreateByAssign = this.jdbcFindList(sql, ClusterConfig.class, params);
		clustersCreateByAssign.addAll(clustersCreateBySelf);
		
		return clustersCreateByAssign;
	}

	@Override
	public List<ClusterConfig> findByClusterCode(Long id, String clusterCode, Long userId) {
		String columnsName = ClassInfoUtils.getColumnsName(ClusterConfig.class);
		String tableName = ClassInfoUtils.getTableName(ClusterConfig.class);
		StringBuilder sql = new StringBuilder(" SELECT ");
		sql.append(columnsName).append(" FROM ").append(tableName).append(" WHERE 1=1 ");
		
		List<Object> params = new ArrayList<Object>();
		if (id != null) {
			sql.append(" AND ID != ? ");
			params.add(id);
		}
		sql.append(" AND CLUSTER_CODE = ? ");
		params.add(clusterCode);
		sql.append(" AND CREATE_USER_ID = ? ");
		params.add(userId);
		List<ClusterConfig> list = this.jdbcFindList(sql.toString(), ClusterConfig.class, params);
		
        return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ctg.itrdc.imageconsole.data.dao.IClusterConfigDao#findByMonitorUrl(
	 * java.lang.String)
	 */
	@Override
	public List<ClusterConfig> findByMonitorUrl(String monitorUrl) {
		String columnsName = ClassInfoUtils.getColumnsName(ClusterConfig.class);
		String tableName = ClassInfoUtils.getTableName(ClusterConfig.class);
		StringBuilder sql = new StringBuilder(" SELECT ");
		sql.append(columnsName).append(" FROM ").append(tableName).append(" WHERE 1=1 ");

		List<Object> params = new ArrayList<Object>();
		sql.append(" AND MONITOR_URL = ? ");
		params.add(monitorUrl);
		List<ClusterConfig> list = this.jdbcFindList(sql.toString(), ClusterConfig.class, params);

		return list;
	}

	@Override
	public ClusterConfig queryByClusterCode(String clusterCode) {
		ClusterConfig clusterConfig = new ClusterConfig();
		clusterConfig.setClusterCode(clusterCode);

		List<ClusterConfig> list = super.selectByExample(clusterConfig);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public String selectApiServerUrlByClusterCode(String clusterCode) {
		ClusterConfig clusterConfig = new ClusterConfig();
		clusterConfig.setClusterCode(clusterCode);
		List<ClusterConfig> list = super.selectByExample(clusterConfig);
		if (list.size() > 1)
			throw new RuntimeException("Error : selecting cluster_config by cluster_code returns more than 1 records");
		if (list.size() == 0)
			return null;
		return list.get(0).getApiServerUrl();
	}

	@Override
	public String selectMonitorUrlByClusterCode(String clusterCode) {
		ClusterConfig clusterConfig = new ClusterConfig();
		clusterConfig.setClusterCode(clusterCode);
		List<ClusterConfig> list = super.selectByExample(clusterConfig);
		if (list.size() > 1)
			throw new RuntimeException("Error : selecting cluster_config by cluster_code returns more than 1 records");
		if (list.size() == 0)
			return null;
		return list.get(0).getMonitorUrl();
	}
}
